package tree;

import java.io.Serializable;

/**
 * Node for 2-3 Tree. Contain 2 Value, with 3 Child.
 * @author channisa
 *
 */
public class Node implements Serializable{
	/**
	 * the less value of node.
	 */
	private int value_left;
	
	/**
	 * big value of node
	 */
	private int value_right;
	
	/**
	 * left child. contain value less than value_left of parent
	 */
	private Node node_left;
	
	/**
	 * middle child, contain value between value_left and value_right of parent.
	 */
	private Node node_middle;
	
	/**
	 * right child. contain value bigger than value_right of parent.
	 */
	private Node node_right;
	
	/**
	 * constructor
	 * @param value_left
	 */
	public Node(int value_left) {
		this.value_left = value_left;
	}
	
	
	//==========================================getter setter==========================================
	
	/**
	 * get value left of the node
	 * @return value left of the node
	 */
	public int getValue_left() {
		return value_left;
	}

	/**
	 * set value left of the node 
	 * @param value_left
	 */
	public void setValue_left(int value_left) {
		this.value_left = value_left;
	}

	/**
	 * get value right of the node
	 * @return value right of the node
	 */
	public int getValue_right() {
		return value_right;
	}

	/**
	 * set value right of the node 
	 * @param value_right
	 */
	public void setValue_right(int value_right) {
		this.value_right = value_right;
	}

	/**
	 * get left child of the Node
	 * @return left child of the node
	 */
	public Node getNode_left() {
		return node_left;
	}

	/**
	 * set left child of the node
	 * @param node_left
	 */
	public void setNode_left(Node node_left) {
		this.node_left = node_left;
	}

	/**
	 * get middle child of the node
	 * @return middle child of the node
	 */
	public Node getNode_middle() {
		return node_middle;
	}

	/**
	 * set middle child of the node
	 * @param node_left
	 */
	public void setNode_middle(Node node_middle) {
		this.node_middle = node_middle;
	}

	/**
	 * get right child of the node
	 * @return right child of the node
	 */
	public Node getNode_right() {
		return node_right;
	}
	
	/**
	 * set right child of the node
	 * @param node_left
	 */
	public void setNode_right(Node node_right) {
		this.node_right = node_right;
	}
		
	//=================================another method=================================
	/**
	 * checking weather node has one value (empty) or two value (full)
	 * @return true if node is full, false if node is empty (right value = null)
	 */
	public boolean isFull(){
		return (getValue_right()!=0);
	}
	
	/**
	 * check weather the node is leaf, or not.
	 * @return true if node is leaf, false if node have child.
	 */
	public boolean isLeaf(){
		return(getNode_left()==null);
	}
	
	/**
	 * update posting list contained in the node.
	 * @param x value of posting list that will be added, docID for inverted index --> adding to list document that has x value (x term)
	 */
	public void updatePostingList(int x, int docID){
		
	}
	
	/**
	 * Set value to node. This method will automatically locating the value.
	 * If the new value is bigger than existing value, then it will set to value_right.
	 * else it will set to value_left, and swap existing value.
	 * @param x, the value that will be added
	 */
	public void setValue(int x){
		if (x>value_left){
			setValue_right(x);
		} else {
			setValue_right(getValue_left());
			setValue_left(x);
		}
	}
	
}
